export function addZero(val) {
    return parseInt(val) > 9 ? val : "0" + val
}
export const WEEK_LIST = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
//获取系统时间年月日
export function getToDay(val) {
    const date =val? new Date(val):new Date(),
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        d = date.getDate()
    return year + "-" + addZero(month) + "-" + addZero(d)
}

export function formatDate(val) {
    const date = new Date(val),
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        d = date.getDate(),
        hours = date.getHours(),
        m = date.getMinutes(),
        s = date.getSeconds()
    return year + "-" + addZero(month) + "-" + addZero(d) + " " + addZero(hours) + ":" + addZero(m) + ":" + addZero(s)
}
//几天后的时间
export function setDate(dateVal) {
    const date = new Date()
    date.setDate(date.getDate() + dateVal)
    return getToDay(date)
}
const MAX_DAYS = [0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
function getMonthDaysFn(month, year) {
    return month != 2 ? MAX_DAYS[month] : (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0 && year % 100 === 0) ? 29 : 28
}
//几个月后的日期
export function setMonth(monthVal, dateVal) {
    const date = new Date(dateVal),
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        d = date.getDate(),
        setY = Math.floor(Math.abs(monthVal) / 12),
        setM = Math.floor(Math.abs(monthVal) % 12)
    let newYear = year,
        newMonth = month
    monthVal > 0 ? ((newYear += setY), (newMonth += setM)) : ((newYear -= setY), (newMonth -= setM))
    newMonth >= 12 && ((newYear += 1), (newMonth = -11))
    newMonth <= 0 && ((newYear -= 1), (newMonth = 12 + newMonth))
    const newDay = Math.min(d, getMonthDaysFn(newMonth, newYear))
    return newYear + "-" + addZero(newMonth) + "-" + addZero(newDay)
}
//几年后的日期
export function setYear(yearVal, dateVal) {
    const date = new Date(dateVal),
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        d = date.getDate()
    let newYear = year + yearVal
    const newDay = Math.min(d, getMonthDaysFn(month, newYear))
    return newYear + "-" + addZero(month) + "-" + addZero(newDay)
}
/*
    获取两个时间之前的时间
*/
export function getDatesBetween(startDate, endDate) {
    const dates = [],currentDate = new Date(startDate),end = new Date(endDate);

    // 循环直到当前日期超过结束日期
    while (currentDate <= end) {
        // 格式化日期为 YYYY-MM-DD
        const year = currentDate.getFullYear();
        const month = addZero(currentDate.getMonth() + 1); // 月份从 0 开始
        const day = addZero(currentDate.getDate());
        dates.push(`${year}-${month}-${day}`);
        // 递增一天
        currentDate.setDate(currentDate.getDate() + 1);
    }
    return dates;
}
